﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


namespace WebLicenseGenerator.UC
{
	public partial class Int32TextBox : System.Web.UI.UserControl
	{
		bool _validateEmptyText = true;

		public bool ValidateEmptyText
		{
			get { return _validateEmptyText; }
			set { _validateEmptyText = value; }
		}
		public string Text
		{
			get { return mskTxt.Text; }
			set { mskTxt.Text = value; }
		}
		public int Number
		{
			get { return Int32.Parse(mskTxt.Text); }
		}
		public int MaxLength
		{
			get { return mskTxt.MaxLength; }
			set { mskTxt.MaxLength = value; }
		}
		public bool IsEmpty
		{
			get { return (String.IsNullOrEmpty(this.Text)); }
		}
		public bool IsEnabled
		{
			get { return mskTxt.Enabled; }
			set { mskTxt.Enabled = value; }
		}
		public bool IsValid
		{
			get
			{
				cvInvalidText.Validate();

				if (ValidateEmptyText)
				{
					cvBlankText.Validate();
					return (cvBlankText.IsValid && cvInvalidText.IsValid);
				}
				else
					return cvInvalidText.IsValid;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected void ValidateBlankText(object sender, ServerValidateEventArgs args)
		{
			args.IsValid = !(this.IsEmpty);
		}

		protected void ValidateInvalidText(object sender, ServerValidateEventArgs args)
		{
			if (!this.IsEmpty)
				args.IsValid = ValidateText();
			else
				args.IsValid = true;
		}

		bool ValidateText()
		{
			try
			{
				Int32.Parse(this.Text);
			}
			catch
			{
				return false;
			}

			return true;
		}
	}
}